<template>
	<!-- 财务对账 -->
	<view class="page-body">
		<!-- 页面内容开始 -->

		<!-- 表格搜索组件开始 -->
		<vk-data-table-query v-model="queryForm1.formData" :columns="queryForm1.columns" @search="search">
		</vk-data-table-query>
		<!-- 表格搜索组件结束 -->

		<view style="margin: 40rpx 0">
			<!-- <el-radio-group v-model="radio1" size="medium" @change="timechange">
				<el-radio-button label="本月"></el-radio-button>
				<el-radio-button label="上个月"></el-radio-button>
			</el-radio-group> -->
			<text style="color: red;margin:0 40rpx;">(时间默认值为当天时间)</text>
			<text style="color: gray;margin:0 40rpx;">充值金额：{{money.all}}</text>
			<!-- 			<text style="color: gray;margin:0 40rpx;">代理佣金比例：{{money.yjbl}}%</text>
			<text style="color: gray;margin:0 40rpx;">预计佣金：{{money.yjyj}}</text> -->

			<text style="color: gray;margin:0 40rpx;"> </text>

			<el-button type="success" size="small" @click="exportData()">导出筛选结果
			</el-button>

		</view>

		<!-- 表格组件开始 -->
		<vk-data-table ref="table1" :action="table1.action" :columns="table1.columns" :query-form-param="queryForm1"
			:right-btns="['detail_auto','more']" :right-btns-more="table1.rightBtnsMore" right-btns-align="right"
			:row-no="true" :pagination="true">
		</vk-data-table>
		<!-- 表格组件结束 -->

		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	var that; // 当前页面对象
	var vk = uni.vk; // vk实例
	var originalForms = {}; // 表单初始化数据
	let {
		todayStart
	} = vk.pubfn.getCommonTime(new Date());
	export default {
		data() {
			// 页面数据变量
			return {
				radio1: "本月",
				money: {
					all: 0,
					yjyj: 0,
					yjbl: 0
				},
				// 页面是否请求中或加载中
				loading: false,
				// init请求返回的数据
				data: {

				},
				// 表格相关开始 -----------------------------------------------------------
				table1: {
					rightBtnsMore: [{
							title: '订单退款',
							disabled: (item) => {
								//控制该按钮对哪些数据生效
								if (item.pay_status == 1) {
									//0：未支付  1：已支付  2：已退款
									return false; //状态为已支付，就不启动禁用功能
								} else {
									return true; //否则，禁用该按钮
								}
							},
							onClick: (item) => {
								//先用弹窗填写退款金额
								this.$prompt('此处单位是分。此处填100，退款金额为1元', '请输入退款金额', {
									confirmButtonText: '确定',
									cancelButtonText: '取消',
									inputPattern: /^[0-9]*$/,
									inputErrorMessage: '退款金额只能为正整数'
								}).then(({
									value
								}) => {
									if (value < 1 || value > item.shifu_amount) {
										this.$message.error('退款金额不能为空且不能超过实付金额');
										return;
									}
									const virtualPayment = item.virtualPayment || 0

									if (item.os_name !== 'ios' && item.virtualPayment) {
										console.log("机型", item.os_name);

										vk.callFunction({
											url: 'admin/pages/kh/yhsj/czjl/xnzf/refund',
											title: '请求中...',
											data: {
												order_fee: value || item
													.shifu_amount,
												order_id: item.out_trade_no,
												openid: item.openid,
												user_id: item.user_id,
											},
											success: (res) => {
												console.log('退款返回信息', res);

												let order_fee = res.order_fee
												let pay_order_id = res.refundOrder.pay_order_id
												// 修改订单状态
												vk.callFunction({
													url: 'admin/pages/kh/yhsj/czjl/xnzf/refundSuc',
													title: '请求中...',
													data: {
														order_fee: order_fee,
														pay_order_id: pay_order_id
													},
													success: (res) => {
														console.log('退款成功', res);
														this.$message({
															message: res
																.msg,
															type: 'success'
														});
														this.refresh()
													}
												});

											}
										});

									} else {
										console.log("机型", item.os_name);
										vk.callFunction({
											url: 'admin/pages/kh/yhsj/czjl/refund',
											title: '请求中...',
											data: {
												out_trade_no: item.out_trade_no,
												refund_desc: "后台操作退款",
												refund_fee: value || item.shifu_amount,
											},
											success: (data) => {
												this.$message({
													message: data.msg,
													type: 'success'
												});
												this.refresh()
											}
										});

									}
								})

							}
						},
						{
							title: '订单回传',
							disabled: (item) => {
								//控制该按钮对哪些数据生效
								if (item.is_backhaul == true || vk.pubfn.isNull(item.tg_pt) ||
									vk.pubfn.isNull(item.clickid) || item.pay_status == 0) {
									return true; //禁用该按钮
								} else {
									return false;
								}
							},
							onClick: (item) => {
								vk.callFunction({
									url: 'client/user/pub/callback_two',
									title: '请求中...',
									data: {
										out_trade_no: item.out_trade_no,
										total_fee: item.shifu_amount,
										_add_time: item._add_time,
										_add_time_str: item._add_time
									},
									success: (data) => {
										//手动回传后刷新列表中的订单数据
										that.$refs.table1.refresh();
									}
								});
							}
						},
						{
							title: '删除订单',
							disabled: (item) => {
								//控制该按钮对哪些数据生效
								if (item.pay_status == 0) {
									//0：未支付  1：已支付  2：已退款
									return false; //状态为未支付，就不启动禁用功能
								} else {
									return true; //否则，禁用该按钮
								}
							},
							onClick: (item) => {
								vk.callFunction({
									url: 'admin/pages/kh/yhsj/czjl/delete',
									title: '请求中...',
									data: {
										_id: item._id
									},
									success: (data) => {
										this.$message({
											message: data.msg,
											type: 'success'
										});
										this.refresh()
									}
								});
							}
						},

					],
					// 表格数据请求地址
					action: "admin/pages/kh/yhsj/czjl/getList",
					// 表格字段显示规则
					columns: [{
							key: "",
							title: "用户信息",
							type: "group",
							minWidth: 240,
							align: "left",
							columns: [{
									key: "user_id",
									title: "用户ID",
									type: "text"
								},
								{
									key: "nickname",
									title: "用户昵称",
									type: "text",
									"defaultValue": "未设置"
								},
								{
									key: "user_register_date",
									title: "注册时间",
									type: "time"
								},
								{
									key: "os_name",
									title: "用户系统类型",
									type: "text"
								},
							],
						},

						{
							"key": "pay_status",
							"title": "支付状态",
							"type": "tag",
							"width": 80,
							data: [{
									value: 0,
									label: "未支付",
									tagType: "danger"
								},
								{
									value: 1,
									label: "已支付",
									tagType: "success"
								},
								{
									value: 2,
									label: "已退款",
									tagType: "warning"
								}
							]
						},

						{
							key: "",
							title: "充值信息",
							type: "group",
							minWidth: 250,
							align: "left",
							columns: [{
									key: "recharge_all",
									title: "支付笔数",
									type: "text"
								},
								{
									key: "recharge_amount",
									title: "充值金额",
									type: "money"
								},
								{
									key: "shifu_amount",
									title: "实付金额",
									type: "money",
									defaultValue: 0
								},
								{
									key: "out_trade_no",
									title: "支付单号",
									type: "text"
								},
								{
									key: "transaction_id",
									title: "微信支付单号",
									type: "text"
								},
							],
						},

						{
							key: "",
							title: "小程序信息",
							type: "group",
							minWidth: 180,
							align: "left",
							columns: [{
									key: "appid",
									title: "appid",
									type: "text"
								},
								{
									key: "appname",
									title: "小程序名称",
									type: "text"
								},
							],
						},

						{
							key: "",
							title: "推广信息",
							type: "group",
							minWidth: 240,
							align: "left",
							columns: [{
									key: "tg_pt",
									title: "推广平台",
									type: "text"
								},
								{
									key: "tfid",
									title: "投放链接id",
									type: "text"
								},
								{
									key: "middleman_id",
									title: "隶属机构",
									type: "text"
								},
								{
									key: "invite_code",
									title: "上级邀请码",
									type: "text"
								},
								{
									key: "tv_name",
									title: "充值来源",
									type: "text"
								},
								{
									key: "ad_id",
									title: "广告账户Id",
									type: "text"
								},
								{
									key: "promotion_id",
									title: "广告计划Id",
									type: "text"
								}
							],
						},
						{
							key: "",
							title: "其他信息",
							type: "group",
							minWidth: 240,
							align: "left",
							columns: [{
									key: "clickid",
									title: "clickid",
									type: "text"
								},
								{
									key: "_add_time",
									title: "添加时间",
									type: "time"
								},
								{
									key: "remark",
									title: "备注",
									type: "text"
								},
								{
									key: "is_backhaul",
									title: "是否回传",
									type: "switch",
									defaultValue: false,
									activeValue: true,
									inactiveValue: false
								},
								{
									key: "noReturnReason",
									title: "订单不回传原因",
									width: 120,
									type: "text"
								},


								// {
								// 	key: "isshow",
								// 	title: "isshow",
								// 	type: "switch",
								// 	defaultValue: false,
								// 	activeValue: true,
								// 	inactiveValue: false
								// },
							],
						},

					],
					// 多选框选中的值
					multipleSelection: [],
					// 当前高亮的记录
					selectItem: ""
				},
				// 表格相关结束 -----------------------------------------------------------
				// 表单相关开始 -----------------------------------------------------------
				// 查询表单请求数据
				queryForm1: {
					// 查询表单数据源，可在此设置默认值
					formData: {
						_add_time: [],
						tv_name: ""
					},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns: [{
							"key": "user_id",
							"title": "用户ID",
							"type": "text",
							"width": 200,
							"mode": "="
						},
						{
							"key": "out_trade_no",
							"title": "订单号",
							"type": "text",
							"width": 200
						},
						{
							"key": "transaction_id",
							"title": "微信单号",
							"type": "text",
							"width": 200
						},
						{
							"key": "appid",
							"title": "小程序",
							"type": "select",
							"width": 200,
							"data": uni.getStorageSync("appid_ARR"),
							"mode": "="
						},
						{
							key: "pay_status",
							title: "支付状态",
							type: "select",
							width: 150,
							data: [{
								value: 0,
								label: "未支付"
							}, {
								value: 1,
								label: "已支付"
							}, {
								value: 2,
								label: "已退款"
							}],
							"mode": "="
						},
						{
							key: "is_backhaul",
							title: "回传状态",
							type: "select",
							width: 150,
							data: [{
								value: false,
								label: "未回传"
							}, {
								value: true,
								label: "已回传"
							}],
							"mode": "="
						},
						// { "key": "appid", "title": "appid", "type": "text", "width": 150, "mode": "=" },
						{
							"key": "tg_pt",
							"title": "推广平台",
							"type": "text",
							"width": 150,
							"mode": "="
						},
						{
							"key": "tfid",
							"title": "投放链接id",
							"type": "text",
							"width": 150,
							"mode": "="
						},
						{
							"key": "invite_code",
							"title": "上级邀请码",
							"type": "text",
							"width": 200,
							"mode": "="
						},
						{
							"key": "tv_name",
							"title": "充值来源",
							"type": "text",
							"width": 200,
							"mode": "="
						},
						{
							"key": "os_name",
							"title": "系统类型",
							"type": "select",
							"data": [{
								"value": "ios",
								"label": "IOS用户"
							}, {
								"value": "android",
								"label": "android用户"
							}, {
								"value": "windows",
								"label": "windows用户"
							}],
							"mode": "="
						},
						{
							key: "_add_time",
							title: "时间",
							type: "datetimerange",
							width: 400,
							mode: "[]"
						}
					]
				},
				form1: {
					// 表单请求数据，此处可以设置默认值
					data: {

					},
					// 表单属性
					props: {
						// 表单请求地址
						action: "",
						// 表单字段显示规则
						columns: [{
								"key": "_id",
								"title": "ID，系统自动生成",
								"type": "text",
								"width": 200
							},
							{
								"key": "user_id",
								"title": "用户ID",
								"type": "text",
								"width": 200
							},
							{
								"key": "nickname",
								"title": "用户昵称",
								"type": "text",
								"width": 200
							},
							{
								"key": "recharge_amount",
								"title": "充值金额",
								"type": "number",
								"width": 200
							},
							{
								"key": "pay_order",
								"title": "支付订单号",
								"type": "text",
								"width": 200
							},
							{
								"key": "invite_code",
								"title": "上级邀请码",
								"type": "text",
								"width": 200
							},
							{
								"key": "remark",
								"title": "备注",
								"type": "text",
								"width": 200
							},
						],
						// 表单验证规则
						rules: {

						},
						// add 代表添加 update 代表修改
						formType: "",
						// 是否显示表单的弹窗
						show: false
					}
				},
				// 其他弹窗表单
				formDatas: {},
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {

		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			//动态变化获取数据
			async getData() {
				await vk.callFunction({
					url: 'admin/pages/kh/yhsj/czjl/getList',
					title: '请求中...',
					data: {
						type: true,
						formData: this.queryForm1.formData
					},
					success: (data) => {
						this.money.all = vk.pubfn.toDecimal(data.sum / 100, 3);
					}
				});
			},
			// 页面数据初始化函数
			init(options) {
				originalForms["form1"] = vk.pubfn.copyObject(that.form1);
				vk.vuex.set('$app.time', vk.pubfn.getCommonTime(new Date()))
				//页面载入时默认加载本月数据
				setTimeout(() => {
					// let monthStart = vk.vuex.get('$app.time.monthStart');
					// let monthEnd = vk.vuex.get('$app.time.monthEnd');
					// this.tongji(monthStart, monthEnd)
					this.getData();
				}, 1000)
			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 表单重置
			resetForm() {
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search() {
				// if (this.queryForm1.formData._add_time && !this.queryForm1.formData.tv_name) {
				// 	let search_starttime = this.queryForm1.formData._add_time[0]
				// 	let search_endtime = this.queryForm1.formData._add_time[1]
				// 	this.tongji(search_starttime, search_endtime)
				// }
				// if (this.queryForm1.formData.tv_name && this.queryForm1.formData._add_time) {
				// 	let recharge_source = this.queryForm1.formData.tv_name
				// 	let search_starttime = this.queryForm1.formData._add_time[0]
				// 	let search_endtime = this.queryForm1.formData._add_time[1]
				// 	this.tongji3(recharge_source, search_starttime, search_endtime)
				// }
				that.$refs.table1.search();
				vk.pubfn.throttle(() => {
					setTimeout(() => {
						// let monthStart = vk.vuex.get('$app.time.monthStart');
						// let monthEnd = vk.vuex.get('$app.time.monthEnd');
						// this.tongji(monthStart, monthEnd)
						this.getData();
					}, 1000)
				}, 1000);

				// this.getData();
			},
			// 刷新
			refresh() {
				that.$refs.table1.refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow() {
				return that.$refs.table1.getCurrentRow();
			},
			async timechange(e) {
				if (e == "本月") {
					let monthStart = vk.vuex.get('$app.time.monthStart');
					let monthEnd = vk.vuex.get('$app.time.monthEnd');
					//同步获取一下充值订单列表
					this.queryForm1.formData._add_time = [] //必须先清空，否则数据会出错
					this.queryForm1.formData._add_time.push(monthStart);
					this.queryForm1.formData._add_time.push(monthEnd);
					this.search()
				}
				if (e == "上个月") {
					let lastMonthStart = vk.vuex.get('$app.time.lastMonthStart');
					let lastMonthEnd = vk.vuex.get('$app.time.lastMonthEnd');
					//同步获取一下充值订单列表
					this.queryForm1.formData._add_time = [] //必须先清空，否则数据会出错
					this.queryForm1.formData._add_time.push(lastMonthStart);
					this.queryForm1.formData._add_time.push(lastMonthEnd);
					this.search()
				}
			},
			async tongji(StartTime, EndTime) {
				vk.callFunction({
					url: 'admin/pages/kh/yhsj/czjl/tongji',
					title: '请求中...',
					data: {
						StartTime: StartTime,
						EndTime: EndTime
					},
					success: (data) => {
						console.log("data=", data);
						//保留*位小数
						this.money.all = vk.pubfn.toDecimal(data.jrczje / 100, 3);
						this.money.yjyj = vk.pubfn.toDecimal(data.yjyj / 100, 3);
						this.money.yjbl = data.yjbl * 100 //佣金比例转为百分比
					}
				});
			},
			async tongji3(recharge_source, StartTime, EndTime) {
				vk.callFunction({
					url: 'admin/pages/kh/yhsj/czjl/tongji3',
					title: '请求中...',
					data: {
						recharge_source: recharge_source,
						StartTime: StartTime,
						EndTime: EndTime
					},
					success: (data) => {
						console.log("data=", data);
						//保留*位小数
						this.money.all = vk.pubfn.toDecimal(data.jrczje / 100, 3);
					}
				});
			},
			exportData() {
				that.$refs.table1.exportExcel({
					fileName: "导出数据",
					title: "正在导出数据...",
					pageIndex: 1,
					pageSize: -1, // 此值为-1，代表导出所有数据
				});
			},
			// uni.showToast的简易封装
			toast(title, icon = "none", mask = false) {
				uni.showToast({
					title,
					icon,
					mask,
					duration: 1500
				});
			},
			// uni.showModal的简易封装
			alert(content, title = "提示") {
				uni.showModal({
					title,
					content,
					showCancel: false
				});
			}
		},
		// 监听属性
		watch: {

		},
		// 过滤器
		filters: {

		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {}

	.red-text {
		color: red;
		margin: 0 40px;
	}
</style>